var id = ""         #此MOD创意工坊ID，可选，暂不使用
var name = "g_ZHPModConfig" #此文件名称
var base = null     #基础工具变量名
var path = null     #本MOD的地址
var Date
var addbox = ""
var Utils
var setMsg#设置弹窗
var setBtn#设置按钮
var window
var helpWindow
var textBox
var rightBtn
var leftBtn

func _ready():
	pass

func _init():
	#读取完毕后初始化，可解决创意工坊后于本地mod调用的问题
	call_deferred("Init")
	call_deferred("setss")

func Init():
	if globalData.infoDs.has("g_ZHPBase"):
		base = globalData.infoDs["g_ZHPBase"]
	if base != null:
		path = chaData.infoDs["cex___zahuopu-Hide"].dir
		Utils = globalData.infoDs["g_ZHPUtils"]
		Date = globalData.infoDs["g_ZHPDate"]
		base.connect("backTitle",self,"backTitle")
		base.connect("onExitGame",self,"load")
		base.setSetting("badao",false)
		base.setSetting("fc",false)
		base.setSetting("CheckMode",false)
		base.loadSetting()
		loadMsg()
		backTitle()

func setss():
	if sys.has_node("/root/Control/Panel2/Panel/Modsetbtn")==false:
		setBtn = Button.new()
		setBtn.text = tr("SETTEXT_1")
		setBtn.name = "Modsetbtn"
		setBtn.connect("pressed",self,"peizhi")
		sys.get_node("/root/Control/Panel2/Panel").add_child(setBtn)

		setMsg=WindowDialog.new()
		setMsg.name="Modset"
		sys.get_node("/root/Control").add_child(setMsg)
		setMsg.set_size(Vector2(500,500))

		var setPanel=Panel.new()
		setPanel.name="Panel"
		setPanel.set_h_size_flags(3)
		setPanel.set_v_size_flags(3)
		setPanel.set_size(Vector2(500,500))
		setPanel.add_stylebox_override("panel",StyleBoxEmpty.new())
		setMsg.add_child(setPanel)

		var msgTittle=Label.new()
		msgTittle.align = 1
		msgTittle.name = "Label"
		msgTittle.text = tr("SETTEXT_2")
		msgTittle.set_anchors_preset(10)
		msgTittle.set_margin(1,-25)
		setPanel.add_child(msgTittle)

		var msgScroll=ScrollContainer.new()
		msgScroll.name="Scroll"
		msgScroll.set_anchors_preset(15)
		msgScroll.set_margin(0,30)
		msgScroll.set_margin(1,20)

		setPanel.add_child(msgScroll)

		var msgVBox=VBoxContainer.new()
		msgVBox.name="Vbox"
		msgVBox.set_custom_minimum_size(Vector2(450,400))
		msgVBox.set_h_size_flags(4)
		msgVBox.set_alignment(1)
		msgVBox.set_draw_behind_parent(true)
		msgScroll.add_child(msgVBox)

	call_deferred("loading")

func peizhi():
	setMsg.popup_centered()

func load():
	setss()
	loadMsg()

func loading():
	addbox = "/root/Control/Modset/Panel/Scroll/Vbox"
	var box = sys.get_node(addbox)
	var phBtn = Button.new()
	phBtn.text = tr("SETTEXT_3")
	phBtn.set_h_size_flags(4)
	phBtn.connect("pressed",self,"loadMenu")
	box.add_child(phBtn)

func loadMsg():
	if base.getSetting("enableMsg", true):
		var bTex = TextureRect.new()
		bTex.set_texture(base.loadImg(path, "other/msg.png"))
		bTex.rect_position = Vector2(550, 720)
		sys.get_node("/root/Control").add_child(bTex)

func loadMenu():
	if base.setting.get("enableMsg") == null:
		base.setting["enableMsg"] = true
	window.popup_centered()

func backTitle():
	#键盘监听
	var keyboard = load("%s/other/Keyboard.gd" % [path]).new()
	keyboard.connect("key_n", self, "loadMenu")
	sys.get_node("/root/Control").add_child(keyboard)
	build()

func build():
	window = WindowDialog.new()
	window.set_size(Vector2(400, 600))
	window.window_title = tr("SETTEXT_4")
	sys.get_node("/root/Control").add_child(window)
	var statisScroll = ScrollContainer.new()
	statisScroll.name = "ScrollContainer"
	statisScroll.set_custom_minimum_size(Vector2(380, 580))
	statisScroll.rect_position = Vector2(50, 50)
	statisScroll.set_margin(0, 50)
	window.add_child(statisScroll)

	var vbox = VBoxContainer.new()
	statisScroll.add_child(vbox)

	#提示设置
	var msg = CheckButton.new()
	msg.text = tr("SETTEXT_5")
	msg.connect("pressed", self, "switchMsg", [msg])
	vbox.add_child(msg)
	if base.getSetting("enableMsg", true):
		msg.pressed = true

	if Date.WorkingTrue:
		#霸道模式
		var badao = CheckButton.new()
		badao.text = tr("SETTEXT_6")
		badao.connect("pressed", self, "Extrabadao", [badao])
		vbox.add_child(badao)
		if base.getSetting("badao", false):
			badao.pressed = true

	
		#紧急支援
		var fc = CheckButton.new()
		fc.text = tr("SETTEXT_6_1")
		fc.connect("pressed", self, "Forcedcall", [fc])
		vbox.add_child(fc)
		if base.getSetting("fc", false):
			fc.pressed = true

	#救援契约
	var zhiyuan = CheckButton.new()
	zhiyuan.text = tr("SETTEXT_7")
	zhiyuan.connect("pressed", self, "Extrazhiyuan", [zhiyuan])
	vbox.add_child(zhiyuan)
	if base.getSetting("zhiyuan", false):
		zhiyuan.pressed = true

	#杂货铺
	var zhp = CheckButton.new()
	zhp.text = tr("SETTEXT_8")
	zhp.connect("pressed", self, "Extrazhp", [zhp])
	vbox.add_child(zhp)
	if base.getSetting("zhp", true):
		zhp.pressed = true
	
	# 杂货铺按钮图标化
	var pict = CheckButton.new()
	pict.text = tr("SETTEXT_11")
	pict.connect("pressed", self, "Picture", [pict])
	vbox.add_child(pict)
	if base.getSetting("picture", false):
		pict.pressed = true

	if !Date.WorkingTrue:
		#防检测
		var CheckMode  = CheckButton.new()
		CheckMode.text = tr("SETTEXT_13")
		CheckMode.connect("pressed", self, "CheckMode", [CheckMode])
		vbox.add_child(CheckMode)
		if base.getSetting("CheckMode", false):
			CheckMode.pressed = true

	#最大天赋数量
	var hbox = HBoxContainer.new()
	Utils.createLabel(tr("SETTEXT_9"), hbox)
	var talents = OptionButton.new()
	talents.add_item("8", 8)
	talents.add_item("9", 9)
	talents.add_item("10", 10)
	talents.add_item("11", 11)
	talents.add_item("12", 12)
	talents.add_item("13", 13)
	talents.add_item("14", 14)
	talents.add_item("15", 15)
	talents.add_item("16", 16)
	hbox.add_child(talents)
	vbox.add_child(hbox)
	talents.connect("item_selected", self, "switchTalents", [talents])
	var talentSetting = base.getSetting("talentSetting", {"index":0,"size":8})
	talents.select(talentSetting.index)

#提示板
func switchMsg(btn):
	base.setSetting("enableMsg", btn.pressed)

#霸道模式
func Extrabadao(btn):
	base.setSetting("badao", btn.pressed)
	if base.setting.get("badao") == true:
		base.setSetting("zhp",false)
		base.setSetting("zhiyuan",false)
		base.setSetting("fc",false)
		base.setSetting("CheckMode",false)
		sys.newBaseMsg(tr("SETTEXT_12"),tr("SETTEXT_10"))

#紧急支援
func Forcedcall(btn):
	base.setSetting("fc", btn.pressed)
	if base.setting.get("fc") == true:
		base.setSetting("zhp",false)
		base.setSetting("zhiyuan",false)
		base.setSetting("badao",false)
		base.setSetting("CheckMode",false)

#救援契约
func Extrazhiyuan(btn):
	base.setSetting("zhiyuan", btn.pressed)
	if base.setting.get("zhiyuan") == true:
		base.setSetting("fc",false)
		base.setSetting("badao",false)
		if base.setting.get("zhp") == true:
			sys.newBaseMsg("",tr("SETTEXT_7_1")+tr("SETTEXT_7_2")+tr("SETTEXT_7_3"))
		elif base.setting.get("zhp") == false:
			sys.newBaseMsg("",tr("SETTEXT_7_1")+tr("SETTEXT_7_2"))

#杂货铺
func Extrazhp(btn):
	base.setSetting("zhp", btn.pressed)
	if base.setting.get("zhp") == true:
		base.setSetting("badao",false)
		base.setSetting("fc",false)

#杂货铺按钮图标化
func Picture(btn):
	base.setSetting("picture", btn.pressed)

#防检测
func CheckMode(btn):
	base.setSetting("CheckMode", btn.pressed)

#最大天赋数
func switchTalents(index, btn):
	base.setSetting("talentSetting", {"index":index,"size":btn.get_selected_id()})
	# if index > 0 && index < 8:
	# 	sys.newBaseMsg("悉知", "1.当天赋槽大于8时，请勿读档\n2.与碧蓝MOD最大天赋数量一起使用时会叠加")
	# if index > 0 && index == 8:
	# 	sys.newBaseMsg("悉知", "1.当天赋槽大于8时，请勿读档\n2.当前所选天赋槽大于15，玩家等级上限提升为16级\n3.与碧蓝MOD最大天赋数量一起使用时会叠加")